use core::ffi;

use ndkm::Window as ANWin;

use super::Window;

#[derive(Clone, Copy)]
pub struct WinRef<'a>(pub Option<&'a ANWin>, pub &'a Win);
impl<'a> types::WinRef<WinRaw> for WinRef<'a> {
	fn raw(&self) -> Option<WinRaw> {
		self.0.map(|anwin| WinRaw::from(anwin.as_sys() as *mut ffi::c_void))
	}
	fn show(&self) {
		// todo!() // send tend
	}
	fn close(&self) {
		// todo!() // send tend
	}
	fn fresh(&self, reason: &str) {
		self.1.dirty(reason);
	}
	fn size(&self) -> (u32, u32) {
		let Some(anwin) = &self.0 else { return (0, 0) };
		let ret = (anwin.width() as _, anwin.height() as _);
		ret
	}
	fn density(&self) -> f32 {
		self.1.scale_factor.unwrap_or(1.0)
	}
}

#[derive(Debug)]
pub struct WinHandle(pub String);
impl types::WinHandle<Window> for WinHandle {
	fn with(&self, fun: impl FnOnce(WinRef<'_>)) {
		with_wins(|wins| {
			for (_, win) in wins.iter().filter(|(_, win)| win.key_ == self.0) {
				fun(win.win_ref());
				break;
			}
		});
	}
}

mod event;
pub use event::*;

mod view;

mod wins;
pub use wins::*;

mod raw;
pub use raw::*;
